<template>
  <div class="visitor-record">
    <van-list
      v-model="loading"
      :finished="finished"
      :immediate-check="false"
      :finished-text="list.length > 0 ? '没有更多了' :''"
      loading-text=" "
      @load="onLoad"
    >
      <!-- 文案提示条件：加载完成 && 没有数据 -->
      <SjTip v-show="finished && list.length===0" :tip-str="'还没有预约信息哦~'"></SjTip>
      <div slot="loading"></div>
      <div v-for="(item,index) in list" :key="index" class="item-wrap">
        <div class="content-wrap">
          <div class="key-item">
            <span v-if="item.interviewedName" class="key-name">被访人姓名</span>
            <span class="key-value special-name ">{{ item.interviewedName }}</span>
          </div>
          <div class="key-item">
            <span class="key-name">被访人电话</span>
            <span class="key-value">{{ item.interviewedPhone || '未填写' }}</span>
          </div>
          <div class="key-item">
            <span class="key-name">预约时间</span>
            <span class="key-value">{{ item.beginTime }} 至<br> {{ item.endTime }}</span>
          </div>
          <div v-if="item.alongWith" class="key-item">
            <span class="key-name">随行人数</span>
            <span class="key-value">{{ item.alongWith }}</span>
          </div>
          <div v-if="item.visitorReason" class="key-item">
            <span class="key-name">访问事由</span>
            <span class="key-value">{{ item.visitorReason }}</span>
          </div>
          <div v-if="item.carNumber" class="key-item">
            <span class="key-name">车辆信息</span>
            <span class="key-value">{{ item.carNumber }}</span>
          </div>
          <div v-if="item.carNumber" class="key-item">
            <span class="key-name">车辆进出</span>
            <span class="key-value">{{ item.allowCar === true ? '允许' : item.allowCar === false ? '拒绝' : '待审核' }}</span>
          </div>
          <div v-if="item.status === 1" class="key-item">
            <span class="key-name">预约码</span>
            <span class="key-value">{{ item.code }}</span>
          </div>
          <div v-if="item.status === 1" class="key-item">
            <span class="key-name">二维码</span>
            <span class="key-value" @click="showQrCode(item.qrCode)">查看</span>
          </div>
          <div v-if="item.status === 2" class="key-item">
            <span class="key-name">拒绝理由</span>
            <span class="key-value">{{ item.rejectReason }}</span>
          </div>
          <div class="key-item">
            <span class="key-name">访问状态</span>
            <span class="key-value">{{ item.visitStatusStr }}
              <span v-if="item.visitStatus === 7 || item.visitStatus === 8" style="color: #267eff;margin-left:10px;" @click="logout(item.id)">点击签离</span>
            </span>
          </div>
        </div>

        <img class="status-icon" :src="require('../../assets/image/icon/' + item.status + '.png')" alt="">

        <div v-if="item.status !== 0" class="btn-again" @click="visitAgain(item)">
          再次拜访
        </div>
      </div>
    </van-list>

    <van-popup v-model="qrCodeDialogVisible">
      <div v-if="qrCodeDialogVisible" ref="qrCodeUrl" class="qrcode"></div>
    </van-popup>

  </div>
</template>

<script>
import vant from 'vant'
import { List, Popup } from 'vant'
import QRCode from 'qrcodejs2'
import SjTip from '@/components/SjTip/index.vue'
import { visitFlowPageAPI, visitFlowLeaveAPI } from '@/axios/api/visitor'
export default {
  name: 'VisitorRecord',
  components: {
    'van-list': List,
    'van-popup': Popup,
    SjTip
  },
  data() {
    return {
      unitId: '', // 单位id
      current: 1,
      total: 0,
      list: [],
      loading: false,
      finished: false,
      qrCodeDialogVisible: false,
      qrCodeImgUrl: ''
    }
  },
  created() {
    document.title = '拜访记录'
    this.unitId = localStorage.getItem('visitorH5_unitId')
    this.getList()
  },
  methods: {
    getList(type) {
      this.loading = true
      vant.Toast.loading({
        duration: 400,
        message: '加载中'
      })
      console.log(`加载第${this.current}页`)
      visitFlowPageAPI({ current: this.current, unitId: this.unitId }).then(res => {
        res.records.forEach((v, i) => {
          v.visitStatusStr = this.dealVisitStatusStr(v.visitStatus)
        })
        this.list.push(...res.records)
        this.total = res.total
        // 判断--是否已全部加载
        if (this.list.length === this.total) {
          this.finished = true
        } else {
          this.current = this.current + 1
        }
        // 结束请求
        this.loading = false
      }).catch(res => {
        this.loading = false
        this.finished = true
      })
    },
    // loading为true时，onLoad失效（保证触底时 只进行一次分页）
    onLoad() {
      console.log('检测到触底，继续请求')
      setTimeout(() => {
        this.getList()
      }, 500)
    },
    visitAgain(item) {
      const info = {
        departmentId: item.departmentId,
        interviewedName: item.interviewedName || '',
        interviewedPhone: item.interviewedPhone
      }
      localStorage.setItem('visitHistory', JSON.stringify(info))
      this.$router.push({ path: '/visitor/visitorSend' })
    },
    showQrCode(str) {
      this.qrCodeDialogVisible = true
      this.$nextTick(() => {
        new QRCode(this.$refs.qrCodeUrl, {
          text: str, // 需要转换为二维码的内容
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
      })
    },
    // 签离状态文案
    dealVisitStatusStr(status) {
      let str = ''
      switch (status) {
        case 0:
          str = '未到访'
          break
        case 5:
          str = '超期自动签离'
          break
        case 6:
          str = '已签离'
          break
        case 7:
          str = '超期未签离'
          break
        case 8:
          str = '已到达'
          break
      }
      return str
    },
    // 点击签离
    logout(id) {
      vant.Dialog.confirm({
        title: '确认签离吗？'
      }).then(() => {
        visitFlowLeaveAPI({ flowId: id, unitId: this.unitId }).then(() => {
          vant.Toast.success('签离成功')
          this.finished = false
          this.current = 1
          this.total = 0
          this.list = []
          this.getList()
        }).catch(res => {
          vant.Toast(res.data.detail || '签离失败')
        })
      })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style lang="scss">
.visitor-record {
  .van-popup {
    display: flex;
    justify-content: center;
    background: transparent;
    .qrcode {
      img {
        background-color: #fff;
        padding: 10px;
      }
    }
  }
}
</style>
<style scoped lang='scss'>
@mixin one-line {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.visitor-record {
  padding: 12px 15px 0;

  .item-wrap {
    position: relative;
    margin-top: 12px;
    padding: 24px 15px 30px;
    border-radius: 12px;
    box-shadow: 0px 3px 6px rgba(38, 126, 255, 0.16);
    background: #ffffff;

    .company-wrap {
      height: 28px;
      width: 100%;
      display: flex;
      align-items: center;
      img {
        width: 28px;
        height: 28px;
        min-width: 28px;
        margin-right: 6px;
      }
      .company-name {
        height: 28px;
        font-size: 18px;
        font-weight: 500;
        display: flex;
        align-items: center;
        flex-grow: 1;
        width: 0;
        span {
          @include one-line;
        }
      }
    }

    .content-wrap {
      padding-top: 16px;

      .key-item:nth-child(1) {
        margin-top: 0;
      }
      .key-item {
        margin-top: 12px;
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        span {
          line-height: 20px;
          font-size: 14px;
        }
        .key-name {
          margin-right: 20px;
          width: 72px;
          min-width: 72px;
          color: #707070;
        }

        .key-value {
          width: 100%;
        }
        .special-name {
          padding-right: 60px;
        }
      }
    }

    .status-icon {
      position: absolute;
      top: 51px;
      right: 24px;
      width: 50px;
      height: 42px;
    }

    .btn-again {
      margin: 16px auto 0;
      width: 120px;
      height: 32px;
      line-height: 32px;
      text-align: center;

      color: #267eff;
      border: 1px solid #267eff;
      border-radius: 24px;
    }
  }
  .item-wrap:nth-child(1) {
    margin-top: 0;
  }
}
</style>
